import { ITodoData } from "../typing";
import TodoDom from "./TodoDom";

class TodoEvent extends TodoDom {
    private todoData: ITodoData[]
    constructor(todoData: ITodoData[], todoWrapper: HTMLElement) {
        super(todoWrapper)
        this.todoData = todoData

        this.initList(this.todoData)
    }

    public addTodo(todoItem: ITodoData) {
        const addIndex: number = this.todoData.findIndex((todo: ITodoData) => todo.content == todoItem.content)
        if (addIndex == -1) {
            this.todoData.push(todoItem)
            this.addItem(todoItem)
            return
        }
        return alert('内容重复');
    }
    public removeTodo(id: number, target: HTMLElement) {
        this.todoData = this.todoData.filter((todo: ITodoData) => todo.id !== id)
        this.removeItem(target)
    }
    public toggleComplete(id: number, target: HTMLElement) {
        this.todoData = this.todoData.map((todo: ITodoData) => {
            if (todo.id === id) {
                todo.completed = !todo.completed
                this.changeCompleted(target, todo.completed)
            }
            return todo
        })

    }
}

export default TodoEvent